package cool.tenyears.common.method;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cool.tenyears.common.constant.UserConstant;
import cool.tenyears.domain.User;

/**
 * @Author tenyears
 * @Description 用户通用方法
 * @Date 2021/6/17 21:55
 */
public class UserMethod {
    /**
     * 为用户添加盐和加密密码
     * @param user 用户信息(不做空校验)
     */
    public static void setPwd(User user) {
        String secret = RandomUtil.randomString(RandomUtil.BASE_CHAR_NUMBER, 8);
        String secretPwd = SaSecureUtil.md5BySalt(user.getPassword(), secret + user.getLoginUser());
        user.setSecret(secret);
        user.setPassword(secretPwd);
    }

    /**
     * 判断密码是否正确
     * @param user              用户
     * @param loginPassword     登录密码
     * @return boolean
     */
    public static boolean checkPassword(User user, String loginPassword) {
        String secretPwd = SaSecureUtil.md5BySalt(loginPassword, user.getSecret() + user.getLoginUser());
        return StrUtil.equals(secretPwd, user.getPassword());
    }

    /**
     * 判断用户状态是否正常（未删除 + 未禁用）
     * @param user 用户
     * @return boolean
     */
    public static  boolean userIsOk(User user) {
        return UserConstant.USER_NO_DELETED_0 == user.getDeleted() && UserConstant.USER_STATUS_OK_0 == user.getUserStatus();
    }

    private UserMethod() {
    }
}
